<template>
  <el-card shadow="hover" class="mt-20">
    <!-- 添加按钮的禁用看三级Id是否有值-->
    <el-button
      type="primary"
      :icon="Plus"
      @click="showAddSpu"
      :disabled="!categoryStore.category3Id"
      v-permission="'btn.Spu.add'"
      >添加SPU</el-button
    >
    <el-table :data="spuList" border class="mt-20">
      <el-table-column label="序号" width="80" type="index" align="center" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            :icon="Plus"
            title="添加SKU"
            @click="showAddSku(row)"
            v-permission="'btn.Spu.addsku'"
          ></el-button>
          <el-button
            type="warning"
            size="small"
            :icon="Edit"
            title="修改SPU"
            @click="showUpdateSpu(row)"
            v-permission="'btn.Spu.update'"
          ></el-button>
          <el-button
            type="info"
            size="small"
            :icon="InfoFilled"
            title="查看SKU列表"
            @click="showSkuList(row.id, row.spuName)"
            v-permission="'btn.Spu.skus'"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            title="删除SPU"
            v-permission="'btn.Spu.delete'"
            @click="showDeleteSpu(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mt-20"
      :total="total"
      v-model:current-page="current"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9]"
      @current-change="getSpuList"
      @size-change="getSpuList"
      layout="prev,pager,next,jumper,->,sizes,total"
    ></el-pagination>
    <el-dialog :title="`${spuName} => SKU列表`" v-model="dialogTableVisible">
      <el-table :data="skuList" border max-height="300" v-loading="skuLoading">
        <el-table-column label="名称" prop="skuName" />
        <el-table-column label="价格(元)" prop="price" />
        <el-table-column label="重量(kg)" prop="weight" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <img
              :src="row.skuDefaultImg"
              :alt="row.skuName"
              class="SpuList__img"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { Plus, Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import { getSpuListApi, removeSpuApi } from "@/api/product/spu";
import { getSkuListBySpuIdApi } from "@/api/product/sku";
import { SkuList } from "@/api/product/model/skuModel";
import { SpuList } from "@/api/product/model/spuModel";
import { useCategoryStore } from "@/stores/category";
import { ref, watch } from "vue";
import { SpuItem } from "@/api/product/model/spuModel";
import { ElMessage, ElMessageBox } from "element-plus";
import { useSpuStore } from "@/stores/spu";
const spuStore = useSpuStore();

const emit = defineEmits(["setIsShow"]);
// 显示添加spu组件
const showAddSpu = () => {
  emit("setIsShow", 2);
  spuStore.$reset();
};
const showUpdateSpu = (row: SpuItem) => {
  //显示修改spu组件
  emit("setIsShow", 2);
  spuStore.spu = row;
};
const showAddSku = (row: SpuItem) => {
  //显示添加sku组件
  emit("setIsShow", 3);
  spuStore.spu = row;
};
// 通过spuId获取skuList
const dialogTableVisible = ref(false);
const skuList = ref<SkuList>([]);
const spuName = ref("");
const skuLoading = ref(false);
const showSkuList = async (id: number, name: string) => {
  dialogTableVisible.value = true;
  skuLoading.value = true;
  skuList.value = await getSkuListBySpuIdApi(id);
  spuName.value = name;
  skuLoading.value = false;
};

// 分页数据
const total = ref(0);
const current = ref(1);
const pageSize = ref(3);
const loading = ref(false);
// 定义spu列表数据
const categoryStore = useCategoryStore();
const spuList = ref<SpuList>();
const getSpuList = async () => {
  loading.value = true;
  const res = await getSpuListApi({
    page: current.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  spuList.value = res.records;
  total.value = res.total;
  // pageSize.value = res.size;
  // current.value = res.current;
  loading.value = false;
};
// 监视三级Id的改变获取分页列表
watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      pageSize.value = 3;
      total.value = 0;
      current.value = 1;
      return;
    }
    getSpuList();
  },
  {
    immediate: true,
  }
);
// 删除Spu
const showDeleteSpu = async (row: SpuItem) => {
  ElMessageBox.confirm(`你确定要删除 ${row.spuName} 吗?`, "删除spu", {
    confirmButtonText: "确定",
    cancelButtonText: "关闭",
    type: "warning",
  })
    // console.log(row.id);
    .then(async () => {
      await removeSpuApi(row.id as number);
      ElMessage({
        type: "success",
        message: "删除spu成功~",
      });
      getSpuList();
    })
    .catch((err) => {
      //
      // console.log(err);
    });
};
</script>
<style scoped>
.SpuList__img {
  width: 100px;
}
</style>
